/*
 * GameRoomPanel.java
 *
 * Created on 25 maart 2008, 14:49
 */
package client.gui.controls;

import client.gui.controls.GameTableModel;
import java.awt.FlowLayout;
import java.util.Enumeration;
import java.util.Hashtable;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.table.TableColumn;
import data.Gameroom;

/**
 *
 * @author  Gonyo
 */
public class GameRoomPanel extends javax.swing.JPanel {

    /** Creates new form GameRoomPanel */
    private JTable gameTabel;
    private Hashtable<String, Gameroom> gamerooms = new Hashtable<String, Gameroom>();

    public GameRoomPanel() {
        updateGameroomLijst();
    }

    public void addGameroom(Gameroom gr) {
        getGamerooms().put(gr.getGamenaam(), gr);
        updateGameroomLijst();
    }

    public void updateGameroomLijst() {
        // opvullen data voor tabel
        GameTableModel model = new GameTableModel();

        Object[][] data = new Object[gamerooms.size()][5];
        Enumeration<String> e = getGamerooms().keys();
        int i = 0;
        while (e.hasMoreElements()) {

            Gameroom g = getGamerooms().get(e.nextElement());
            data[i][0] = g.getGamenaam();
            data[i][1] = g.getType().toString();
            data[i][2] = g.getNicknameOprichter();
            data[i][3] = g.getAantalSpelers() + "/" + g.getMaxAantalSpelers();
            data[i][4] = !g.getPaswoord().equals("null");
            i++;
        }

        model.setData(data);

        gameTabel = new JTable(model);
        gameTabel.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

        gameTabel.getColumnModel().getColumn(0).setPreferredWidth(70);
        gameTabel.getColumnModel().getColumn(1).setPreferredWidth(50);
        gameTabel.getColumnModel().getColumn(2).setPreferredWidth(70);
        gameTabel.getColumnModel().getColumn(3).setPreferredWidth(10);
        gameTabel.getColumnModel().getColumn(4).setPreferredWidth(15);

        this.setLayout(new FlowLayout());
        JScrollPane scrollPane = new JScrollPane(gameTabel);

        //Alle componenten van het panel verwijderen, daarna toevoegen, en vervolgens
        //het scherm hertekenen
        this.removeAll();
        this.add(scrollPane);
        this.revalidate();
    }

    public Gameroom getGeselecteerdeRoom() {
        int rij = gameTabel.getSelectedRow();
        if (rij != -1) {
            return gamerooms.get(gameTabel.getValueAt(rij, 0).toString());
        } else {
            return null;
        }
    }

    public Hashtable<String, Gameroom> getGamerooms() {
        return gamerooms;
    }

    public void setGamerooms(Hashtable<String, Gameroom> gamerooms) {
        this.gamerooms = gamerooms;
    }
}
